package com.webapp.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;

import com.webapp.domain.entity.Dict;

/**
 * 字典类
 * @author zern
 *
 */
@Repository
public class DictDAO extends AbstractDAO<Dict> {

	@SuppressWarnings("unchecked")
	public List<Dict> findListByType(String type){
		String hql = "from Dict d where d.type = :type";
		Query query = getSession().createQuery(hql).setString("type", type);
		return query.list();
		
	}

	public Dict getListByTypeANDDesc(String type,String desc){
		String hql = "from Dict d where d.type = :type and d.describes=:describes";
		Query query = getSession().createQuery(hql).setString("type", type).setString("describes", desc);
		return (Dict)query.list().get(0);
		
	}
	
	public int deleteALlByType(String type){
		String hql = "delete Dict d where d.type = :type";
		Query query = getSession().createQuery(hql).setString("type", type);
		return query.executeUpdate();
	}
	
	public Dict getByCriteria(DetachedCriteria criteria) {
        List<Dict> list = findByCriteria(criteria);
        return list.size() > 0 ? list.get(0) : null;
    }
	
	public Dict getDictByTypeAndCode(String type,String code){
		return this.get("where dict.type=? and dict.code=?", type,code);
	}
	
	
}
